package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.Md5Utils;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import sun.rmi.runtime.Log;

import java.util.List;


/**
 * 业务实现类：员工
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private LogininfoMapper logininfoMapper;
    
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void add(Employee employee) {
        //1.处理密码和盐值
        //盐值
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        //密码 - 加盐加密之后的
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5Pwd);

        //2.保存logininfo信息
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);

        //3.保存员工信息
        employee.setLogininfo_id(logininfo.getId());
        super.add(employee);//添加成功之后 - 自增长的id

        //添加中间表的数据
        if(employee.getRole_id()!=null){
            employeeMapper.addEmployeeRole(employee);
        }
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    @Override
    @Transactional
    public void del(Long id) {
        //根据id查询员工对象
        Employee employee = employeeMapper.loadById(id);
        //删除员工对象
        super.del(id);
        //删除对应的Logininfo信息
        logininfoMapper.remove(employee.getLogininfo_id());

        //删除中间表的数据
        employeeMapper.delEmployeeRoleByEmpId(id);
    }

    @Override
    @Transactional
    public void update(Employee employee) {//employee页面中传递过来的
        //1.处理密码和盐值
        //盐值
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        //密码 - 加盐加密之后的
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5Pwd);

        //2.修改员工信息
        super.update(employee);

        //3.修改logininfo信息：通过id【logininfo_Id】去修改
        Employee oldEmployee = employeeMapper.loadById(employee.getId());
        Logininfo oldLogininfo = logininfoMapper.loadById(oldEmployee.getLogininfo_id());
        //5个同名属性就是最新的数据
        BeanUtils.copyProperties(employee,oldLogininfo);
        oldLogininfo.setId(oldEmployee.getLogininfo_id());
        logininfoMapper.update(oldLogininfo);

        //先删除以前的中间数据
        employeeMapper.delEmployeeRoleByEmpId(employee.getId());
        //再添加新的数据
        if(employee.getRole_id()!=null) {
            employeeMapper.addEmployeeRole(employee);
        }

    }
}

